package com.cdu.ai_intelligent_teaching_and_training_system.service;

import com.cdu.ai_intelligent_teaching_and_training_system.entity.Course;
import com.cdu.ai_intelligent_teaching_and_training_system.mapper.CourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CourseService {
    
    @Autowired
    private CourseMapper courseMapper;
    
    public List<Course> findAll() {
        return courseMapper.selectAll();
    }
    
    public Optional<Course> findById(Long id) {
        return Optional.ofNullable(courseMapper.selectById(id));
    }
    
    public List<Course> findByTeacherId(Long teacherId) {
        return courseMapper.selectByTeacherId(teacherId);
    }
    
    // MyBatis不支持自动like，需要在Mapper.xml中自定义模糊查询
    public List<Course> findByNameContaining(String name) {
        // 这里假设selectByNameLike已在xml中实现
        return courseMapper.selectByNameLike("%" + name + "%");
    }
    
    public Course save(Course course) {
        if (course.getCourseId() == null) {
            courseMapper.insert(course);
        } else {
            courseMapper.update(course);
        }
        return course;
    }
    
    public void deleteById(Long id) {
        courseMapper.deleteById(id);
    }
} 